package com.example.mybiadushoujiweishi.activity.acivitys;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.mybiadushoujiweishi.R;
import com.example.mybiadushoujiweishi.activity.acivitys.adapter.ViewpagerAdapter;
import com.example.mybiadushoujiweishi.activity.acivitys.base.BaseActivity;
import com.example.mybiadushoujiweishi.activity.acivitys.utils.MyApplication;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.vp_welcome)
    ViewPager vpWelcome;
    @BindView(R.id.ll)
    LinearLayout ll;

    ViewpagerAdapter adapter;
    List<View> mViewlists;
    @BindView(R.id.text_tiaoguo)
    TextView textTiaoguo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //  setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);
        //ButterKnife.bind(this);
        //ButterKnife.bind(this);


        LayoutInflater inflater = LayoutInflater.from(this);
        mViewlists = new ArrayList<View>();

        mViewlists.add(inflater.inflate(R.layout.guide_onelayout, null));
        mViewlists.add(inflater.inflate(R.layout.guide_twolayout, null));
        mViewlists.add(inflater.inflate(R.layout.guide_threelayout, null));

        adapter = new ViewpagerAdapter(mViewlists);
        vpWelcome.setAdapter(adapter);

        View point = null;
        for (int i = 0; i < mViewlists.size(); i++) {
            point = new View(this);
            point.setEnabled(false);
            //使用标签选择器
            point.setBackgroundResource(R.drawable.activity_select);
            //设置圆点大小
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(15, 15);
            //设置间距
            params.leftMargin = 50;
            //添加view
            ll.addView(point, params);

        }
        //设置第一页为圆点为选中
        ll.getChildAt(0).setEnabled(true);

        // initdestory();
        // vpWelcome.setCurrentItem(R.id.text_tiaoguo);
        vpWelcome.setOnPageChangeListener(this);
        mViewlists.get(2).findViewById(R.id.login_home).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               /* Intent intent = new Intent(GuideActivity.this, MainActivity.class);
                startActivity(intent);
                finish();*/


                //设置共享首选项
                SharedPreferences s = MyApplication.getSharedPreferences();
                SharedPreferences.Editor edits = s.edit();
                edits.putBoolean("login", true);
                edits.commit();

                startActivity(new Intent(GuideActivity.this, WelcomeActivity.class));
                finish();


            }
        });
        textTiaoguo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vpWelcome.setCurrentItem(mViewlists.size() - 1);
            }
        });
    }

    @Override
    public int getContentView() {
        return R.layout.activity_guide;
    }

    @Override
    public void initData() {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        for (int i = 0; i < mViewlists.size(); i++) {
            if (i == position) {
                //img[i].setImageResource(R.mipmap.lgon_noselect);
                ll.getChildAt(i).setEnabled(true);
            } else {
                // img[i].setImageResource(R.mipmap.login_select);
                ll.getChildAt(i).setEnabled(false);
            }
        }
        switch (position) {
            case 0:

                textTiaoguo.setVisibility(View.VISIBLE);
                break;
            case 1:
                textTiaoguo.setVisibility(View.VISIBLE);
                break;
            case 2:
                textTiaoguo.setVisibility(View.INVISIBLE);
                break;
        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
